﻿using System;
using System.Text;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.WebControls;

namespace Voodoo.SharePoint.ReadUnreadMarks
{
    //This a very simple wrapper around the string value
    //the raw data is stored as the string version of 
    //a numerical userID bracketed by a delimiter. In
    //the current code, the delimiter is a . so string  
    //data could look like this :   .1.500.645.3. 
    public class ReadUnreadFieldValue
    {
        string _ReadUnreadData = string.Empty;
        public ReadUnreadFieldValue()
            : this(ReadUnreadField.READ_MARK_DELIMITER)
        {
        }

        public ReadUnreadFieldValue(object fieldData)
        {
            _ReadUnreadData = fieldData as string;
            if (null == _ReadUnreadData) _ReadUnreadData = ReadUnreadField.READ_MARK_DELIMITER;
        }

        public ReadUnreadFieldValue(string fieldData)
        {
            _ReadUnreadData = fieldData;
        }

        virtual public string ToHTMLString()
        {
            return Microsoft.SharePoint.Utilities.SPEncode.HtmlEncode(this._ReadUnreadData);
        }

        public override string ToString()
        {
            return _ReadUnreadData.ToString();
        }

        public override int GetHashCode()
        {
            return _ReadUnreadData.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return _ReadUnreadData.Equals(obj);
        }

        public bool IsEmpty
        {
            get
            {
                if (_ReadUnreadData == ReadUnreadField.READ_MARK_DELIMITER) return true;
                return false;
            }
        }

        public bool HasReadMark(int userId)
        {
            //check for the read mark in the string
            bool returnValue = false;
            if (!string.IsNullOrEmpty(_ReadUnreadData))
            {
                string readMark = ReadUnreadField.READ_MARK_DELIMITER + userId.ToString() + ReadUnreadField.READ_MARK_DELIMITER;
                if (-1 != _ReadUnreadData.IndexOf(readMark))
                {
                    returnValue = true;
                }
            }
            return returnValue;
        }

        public void MarkUserUnread(int userId)
        {
            //remove the read mark, if it exits
            if (0 != _ReadUnreadData.Length)
            {
                _ReadUnreadData = _ReadUnreadData.Replace(ReadUnreadField.READ_MARK_DELIMITER + userId.ToString() + ReadUnreadField.READ_MARK_DELIMITER, ReadUnreadField.READ_MARK_DELIMITER);
            }

        }

        public void MarkUserRead(int userId)
        {
            //add the read mark to the string
            if (0 == _ReadUnreadData.Length)
            {
                _ReadUnreadData = ReadUnreadField.READ_MARK_DELIMITER + userId.ToString() + ReadUnreadField.READ_MARK_DELIMITER;
            }
            else
            {
                _ReadUnreadData += userId.ToString() + ReadUnreadField.READ_MARK_DELIMITER;
            }
        }

        public void ClearUserMarks()
        {
            _ReadUnreadData = ReadUnreadField.READ_MARK_DELIMITER;
        }
    }
}
